import Vue from 'vue'
import VueRouter from 'vue-router'
import LoginPage from '@/components/LoginPage'
import HomePage from '@/components/HomePage'
import WelcomePage from '@/components/WelcomePage'
import UserPage from '@/components/user/UserPage'
import RightsPage from '@/components/power/RightsPage'
import RolesPage from '@/components/power/RolesPage'
import Category from '@/components/goods/Category'
import Params from  '@/components/goods/Params'
import List from  '@/components/goods/List'
import Add from  '@/components/goods/Add'
import Order from  '@/components/order/Order'
import Report from  '@/components/report/Report'

Vue.use(VueRouter)

const routes = [
  // redirect 重定向路由
  {
    path: '/',
    redirect: 'login'
  },
  {
    path: '/login',
    component: LoginPage
  },
  {
    path: '/home',
    component: HomePage,
    redirect: '/welcome',
    children: [
      {
      path: '/welcome',
      component: WelcomePage
      },
      {
        path: '/users',
        component: UserPage
      },
      {
        path: '/rights',
        component: RightsPage
      },
      {
        path: '/roles',
        component: RolesPage
      },
      {
        path: '/categories',
        component: Category
      },
      {
        path: '/params',
        component: Params
      },
      {
        path: '/goods',
        component: List
      },
      {
        path: '/goods/add',
        component: Add
      },
      {
        path: '/orders',
        component: Order
      },
      {
        path: '/reports',
        component: Report
      },
    ]
  }
]

const router = new VueRouter({
  routes
})

// 判断是否成功登录
router.beforeEach((to,from,next)=>{
  if(to.path!=='/login'){
    // 获取sessionStorage中的token
    const tokenStr = window.sessionStorage.getItem('token')
    if(!tokenStr) return next('/login')
    next()
  }else{
    next()
  }
})

export default router
